Theory¶
The wave equation in one-dimensional form is written as
\begin{equation} \frac{\partial u}{\partial t}+c\frac{\partial u}{\partial x}=0, \end{equation}
where \(u\) is the wave distribution in space \(x\) and time \(t\), and \(c\) is wave velocity.
Methods¶
Backward Euler¶
\begin{equation} \frac{u_j^{n+1} - u_j^n}{\Delta t}+c\frac{u_j^n - u_{j-1}^n}{\Delta x} = 0 \end{equation}
putting all \(n+1\) terms on the left
\begin{equation} u_j^{n+1} = u_j^n - \frac{c\Delta t}{\Delta x}(u_j^n - u_{j-1}^n) \end{equation}
Foreward Euler¶
\begin{equation} \frac{u_j^{n+1} - u_j^n}{\Delta t}+c\frac{u_{j+1}^n - u_j^n}{\Delta x} = 0 \end{equation}
\begin{equation} u_j^{n+1} = u_j^n - \frac{c\Delta t}{\Delta x}(u_{j+1}^n - u_{j}^n) \end{equation}
Forward Time Central Space (FTCS)¶
\begin{equation} \frac{u_j^{n+1} - u_j^n}{\Delta t}+c\frac{u_{j+1}^n - u_{j-1}^n}{\Delta x} = 0 \end{equation}
\begin{equation} u_j^{n+1} = u_j^n - \frac{c\Delta t}{2\Delta x}(u_{j+1}^n - u_{j-1}^n) \end{equation}
Leapfrog¶
\begin{equation} u_j^{n+1} = u_j^{n-1} - \frac{c\Delta t}{\Delta x}(u_{j+1}^n - u_{j-1}^n) \end{equation}
Lax-Wendroff¶
\begin{equation} u_j^{n+1} = u_j^n - \frac{c\Delta t}{2\Delta x}(u_{j+1}^n - u_{j-1}^n) + \frac{1}{2}(\frac{c\Delta t}{\Delta x})^{2}(u_{j+1}^n - 2u_j^n + u_{j-1}^n) \end{equation}
Lax¶
\begin{equation} u_j^{n+1} = \frac{u_{j+1}^n + u_{j-1}^n}{2} - \frac{c\Delta t}{2\Delta x}(u_{j+1}^n - u_{j-1}^n) \end{equation}
Hybrid Theta¶
\begin{equation} u_j^{n+1} + \theta(\frac{CFL}{2})(u_{j+1}^{n+1} - u_{j-1}^{n+1}) = u_j^n - (1-\theta)\frac{CFL}{2}(u_{j+1}^n - u_{j-1}^n) \end{equation}
2nd order in time, 4th order in space (T2S4)¶
\begin{equation} u_j^{n+1} = u_j^n - c\frac{\Delta t}{\Delta x}\Big[-\frac{u_{j+2}^n}{12} + \frac{2}{3}u_{j+1}^n -\frac{2}{3}u_{j-1}^n + \frac{u_{j-2}^n}{12}\Big] + \frac{1}{2}\Big(\frac{c\Delta t}{\Delta x}\Big)^2 \Big[-\frac{u_{j+2}^n}{12} + \frac{4}{3}u_{j+1}^n - \frac{5}{2}u_j^n + \frac{4}{3}u_{j-1}^n - \frac{u_{j-2}^n}{12} \Big] \end{equation}
Wave shape¶
Let’s see how the wave moves.
import pandas as pd
import matplotlib.pyplot as plt
df_BE_1 = pd.read_csv('../../testCase/1-BE/results-t=0.00.csv')
df_BE_2 = pd.read_csv('../../testCase/1-BE/results-t=0.62.csv')
df_BE_3 = pd.read_csv('../../testCase/1-BE/results-t=1.30.csv')
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, sharex=True, sharey=True, figsize=(15,3))
y1 = df_BE_1['u']
y2 = df_BE_2['u']
y3 = df_BE_3['u']
ax1.plot(y1);
ax2.plot(y2);
ax3.plot(y3);
df_BE = pd.read_csv('../../testCase/1-BE/results-t=2.35.csv')
df_FE = pd.read_csv('../../testCase/2-FE/results-t=1.30.csv')
df_FTCS = pd.read_csv('../../testCase/3-FTCS/results-t=1.30.csv')
df_LF = pd.read_csv('../../testCase/4-Leapfrog/results-t=1.30.csv')
df_LW = pd.read_csv('../../testCase/5-Lax-Wendroff/results-t=1.30.csv')
df_Lax = pd.read_csv('../../testCase/6-Lax/results-t=1.30.csv')
df_HT = pd.read_csv('../../testCase/7-Hybrid-theta/results-65.csv')
df_T2S4 = pd.read_csv('../../testCase/8-T2S4/results-t=1.30.csv')
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-4-7815b9a20da1> in <module>
1 df_BE = pd.read_csv('../../testCase/1-BE/results-t=2.35.csv')
----> 2 df_FE = pd.read_csv('../../testCase/2-FE/results-t=1.30.csv')
3 df_FTCS = pd.read_csv('../../testCase/3-FTCS/results-t=1.30.csv')
4 df_LF = pd.read_csv('../../testCase/4-Leapfrog/results-t=1.30.csv')
5 df_LW = pd.read_csv('../../testCase/5-Lax-Wendroff/results-t=1.30.csv')
/opt/hostedtoolcache/Python/3.7.9/x64/lib/python3.7/site-packages/pandas/io/parsers.py in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options)
603 kwds.update(kwds_defaults)
604
--> 605 return _read(filepath_or_buffer, kwds)
606
607
/opt/hostedtoolcache/Python/3.7.9/x64/lib/python3.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
455
456 # Create the parser.
--> 457 parser = TextFileReader(filepath_or_buffer, **kwds)
458
459 if chunksize or iterator:
/opt/hostedtoolcache/Python/3.7.9/x64/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
812 self.options["has_index_names"] = kwds["has_index_names"]
813
--> 814 self._engine = self._make_engine(self.engine)
815
816 def close(self):
/opt/hostedtoolcache/Python/3.7.9/x64/lib/python3.7/site-packages/pandas/io/parsers.py in _make_engine(self, engine)
1043 )
1044 # error: Too many arguments for "ParserBase"
-> 1045 return mapping[engine](self.f, **self.options) # type: ignore[call-arg]
1046
1047 def _failover_to_python(self):
/opt/hostedtoolcache/Python/3.7.9/x64/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
1860
1861 # open handles
-> 1862 self._open_handles(src, kwds)
1863 assert self.handles is not None
1864 for key in ("storage_options", "encoding", "memory_map", "compression"):
/opt/hostedtoolcache/Python/3.7.9/x64/lib/python3.7/site-packages/pandas/io/parsers.py in _open_handles(self, src, kwds)
1361 compression=kwds.get("compression", None),
1362 memory_map=kwds.get("memory_map", False),
-> 1363 storage_options=kwds.get("storage_options", None),
1364 )
1365
/opt/hostedtoolcache/Python/3.7.9/x64/lib/python3.7/site-packages/pandas/io/common.py in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
642 encoding=ioargs.encoding,
643 errors=errors,
--> 644 newline="",
645 )
646 else:
FileNotFoundError: [Errno 2] No such file or directory: '../../testCase/2-FE/results-t=1.30.csv'
import matplotlib.ticker as ticker
fig, ((ax1, ax2), (ax3, ax4), (ax5, ax6), (ax7, ax8)) = plt.subplots(4, 2, sharex=True, sharey=True, figsize=(15,14))
ax1.set(ylim=(-0.5, 1.2))
y1 = df_BE['u']
y2 = df_FE['u']
y3 = df_FTCS['u']
y4 = df_LF['u']
y5 = df_LW['u']
y6 = df_Lax['u']
y7 = df_HT['u']
y8 = df_T2S4['u']
ax1.plot(y1)
ax1.set_title('Backward Euler')
ax2.plot(y2, 'tab:orange')
ax2.set_title('Foreward Euler')
ax3.plot(y3, 'tab:green')
ax3.set_title('FTCS')
ax4.plot(y4, 'tab:red')
ax4.set_title('Leapfrog')
ax5.plot(y5, 'tab:purple')
ax5.set_title('Lax-Wendroff')
ax6.plot(y6, 'tab:cyan')
ax6.set_title('Lax')
ax7.plot(y7, 'tab:pink')
ax7.set_title('Hybrid Theta')
ax8.plot(y8, 'tab:brown')
ax8.set_title('F2S4')
ax1.yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
fig = plt.figure();
ax = plt.axes(xlim=(-0.2, 5.5), ylim=(-0.7, 1.4));
line, = ax.plot([], [], lw=2);
def init():
line.set_data([], [])
return line
def animate(i):
filename = "../../testCase/7-Hybrid-theta/results-{}.csv".format(i)
xy = np.genfromtxt(filename, delimiter=",")
x = xy[:,0]; y = xy[:,1]
line.set_data(x, y)
return line
anim = animation.FuncAnimation(fig, animate, init_func = init,
frames=440, interval=40, repeat=False)
plt.close()
HTML(anim.to_jshtml())